###########################################################################
#	$Id: labourFormManager.p 15 2009-04-24 04:14:14Z donbeave $
###########################################################################

@CLASS
labourFormManager

@OPTIONS
locals

#----- Constructor -----

@create[]
  $self._isStatic(false)

#----- Public -----

@add[strLink;altValue]
  $tParts[^strLink.split[.]]

  $hForm[$self.form]

  ^tParts.menu{
    ^if(^tParts.line[] != ^tParts.count[]){
      ^if(!$hForm.[$tParts.piece]){
        $hForm.[$tParts.piece][^hash::create[]]
      }

      $hForm[$hForm.[$tParts.piece]]
    }{
       ^if($hForm.[$tParts.piece] is "table"){
         ^if(!^hForm.[$tParts.piece].locate[field;$altValue]){
           ^hForm.[$tParts.piece].append{$altValue}
         }
       }{
          $aValue[$hForm.[$tParts.piece]]

          $hForm.[$tParts.piece][^table::create{field
$altValue}]

          ^if(def $aValue){
            ^hForm.[$tParts.piece].append{$aValue}
          }
        }
     }
  }

@delete[strLink;altValue]
  $tParts[^strLink.split[.]]

  $hForm[$self.form]

  ^tParts.menu{
    ^if(^tParts.line[] != ^tParts.count[]){
      $hForm[$hForm.[$tParts.piece]]
    }{
       ^if($hForm is "hash"){
         $tContent[$hForm.[$tParts.piece]]

         ^if($tContent is "table"){
           $hForm.[$tParts.piece][^tContent.select($tContent.field ne $altValue)]
         }{
            $hForm.[$tParts.piece][]
          }
       }
     }
  }

@string[hshOptions]
  $hOptions[^hash::create[$hshOptions]]

  ^if(def $hOptions.delimeter){
    $sDelimeter[$hOptions.delimeter]
  }{
     $sDelimeter[&amp^;]
   }

  $result[^_field2string[^if(def $hParams.form){$hParams.form}{$self.form};$sDelimeter]]

  ^if(def $result){
    $result[?^result.mid(0;^result.length[]-^sDelimeter.length[])]
  }

#----- Private -----

@_parseForm[]
## преобразует поля формы в hash (на основании точек в названии)
  $result[^hash::create[]]

  $hForm[$result]

  ^form:fields.foreach[sFieldName;]{
#   разбираем название поля на куски
    $tParts[^sFieldName.split[.]]

    ^tParts.menu{
#     если нынешний кусок не является последним, то создаем дальше структуру хэша
      ^if(^tParts.line[] != ^tParts.count[]){
        ^if(!def $hForm.[$tParts.piece]){
          $hForm.[$tParts.piece][^hash::create[]]
        }

        $hForm[$hForm.[$tParts.piece]]
      }{
#        если имеются свыше одного значения к одному полю, то добавляем в хэш таблицу со всеми значениями
         ^if(^form:tables.[$sFieldName].count[] > 1){
#          создаем копию таблицы (необходимо для add и delete методов)
           $hForm.[$tParts.piece][^table::create[$form:tables.$sFieldName]]
         }{
            $hForm.[$tParts.piece][$form:fields.$sFieldName]
          }
       }
    }

    $hForm[$result]
  }

@_field2string[altData;strDelimeter;_strPrefix;_strResult]
  $result[]

  ^if($altData is hash){
    ^altData.foreach[sName;aContent]{
      $result[${result}^_field2string[$aContent;$strDelimeter;^if(def $_strPrefix){${_strPrefix}.}$sName]]
    }
  }

  ^if($altData is table){
    ^altData.menu{
      $result[${result}^_field2string[$altData.field;$strDelimeter;$_strPrefix]]
    }
  }

  ^if($altData is string || $altData is int || $altData is double){
    $result[$_strPrefix=${altData}$strDelimeter]
  }

#----- Properties -----

@GET_form[]
  ^if(^_isStatic.bool(true)){
    ^if(!def $self._staticForm){
      $self._staticForm[^_parseForm[]]
    }
    $result[$self._staticForm]
  }{
     ^if(!def $self._form){
       $self._form[^_parseForm[]]
     }
     $result[$self._form]
   }